/*****************************************************
 * Copyright ?, 2006-2008 Huawei Technologies Co.,Ltd. 
 * Filename: rotate.cpp
 * Version: 1.0 / Author: Lei Yu / Date: 2007-4-27
 * Description: 
 * Version: 
 * Function List :
 * History:
 * leiyu    2007-4-27   1.0  create this file
 ****************************************************/

#include "precomp.h"

/*****************************************************
 * Function: GetRotateModeFromReg
 * Description: get initial screen rotate angle from regedit
 * Calls: 
 * Called by: 
 * Table Access: list the table you will use. 
 * Table Update: list the table which you will update.
 * Input: NONE
 * Output: NONE
 * Return: rotate angle
 * Others: NONE
 ****************************************************/
int HisiDisp::GetRotateModeFromReg()
{
    HKEY hKey;

    if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SYSTEM\\GDI\\ROTATION"), 0, 0, &hKey))
    {
        DWORD dwAngle;

        DWORD dwType = REG_DWORD;
        DWORD dwSize = sizeof(DWORD);

        if (ERROR_SUCCESS == RegQueryValueEx(hKey,
                                             TEXT("ANGLE"),
                                             NULL,
                                             &dwType,
                                             (LPBYTE)&dwAngle,
                                             &dwSize))
        {
            m_nRotateAngle = dwAngle;
        }
        else
        {
            DISPMSG(GPE_ZONE_HISI, (TEXT("Error to query reg value.\n")));
        }

        if (RegCloseKey(hKey) != ERROR_SUCCESS )
        {
            DISPMSG(GPE_ZONE_HISI, (TEXT("Error to close reg key.\n")));
        }
    }
    else
    {
        DISPMSG(GPE_ZONE_HISI, (TEXT("Error to open reg key.\n")));
        return DMDO_0;
    }

    if (m_bLandScapeMach)
    {
        switch (m_nRotateAngle)
        {
        case 90:
        default:
            return DMDO_0;
        case 180:
            return DMDO_90;
        case 270:
            return DMDO_180;
        case 0:
            return DMDO_270;
        }
    }
    else
    {
        switch (m_nRotateAngle)
        {
        case 90:
            return DMDO_90;  
        case 180:
            return DMDO_180;
        case 270:
            return DMDO_270;
        case 0:
        default:
            return DMDO_0;  
        }
    }
}

/*****************************************************
 * Function: SetRotateParams
 * Description: reset mode info according to rotate angle
 * Calls: 
 * Called by: 
 * Table Access: list the table you will use. 
 * Table Update: list the table which you will update.
 * Input: NONE
 * Output: NONE
 * Return: NONE
 * Others: NONE
 ****************************************************/
void HisiDisp::SetRotateParams()
{   
    switch(m_iRotate)
    {
    case DMDO_90:   
    case DMDO_270:  
        m_pMode->height = m_nScreenWidthSave;
        m_pMode->width = m_nScreenHeightSave;
        break;

    case DMDO_0:    
    case DMDO_180:  
    default:
        m_pMode->width = m_nScreenWidthSave;
        m_pMode->height = m_nScreenHeightSave;
        break;
    }
}

/*****************************************************
 * Function: DynRotate
 * Description: reset screen info according to rotate angle
 * Calls: 
 * Called by: 
 * Table Access: list the table you will use. 
 * Table Update: list the table which you will update.
 * Input: int angle: rotate angle
 * Output: NONE
 * Return: DISP_CHANGE_SUCCESSFUL
 * Others: NONE
 ****************************************************/
LONG HisiDisp::DynRotate(int angle)
{   
    if (angle == m_iRotate)
        return DISP_CHANGE_SUCCESSFUL;

    m_iRotate = angle;

    switch(m_iRotate)
    {
    case DMDO_0:
    case DMDO_180:
        m_nScreenHeight = m_nScreenHeightSave;
        m_nScreenWidth = m_nScreenWidthSave;
        break;
    case DMDO_90:
    case DMDO_270:
        m_nScreenHeight = m_nScreenWidthSave;
        m_nScreenWidth = m_nScreenHeightSave;
        break;
    default:
        break;
    }

    m_pMode->width = m_nScreenWidth;
    m_pMode->height = m_nScreenHeight;

    m_pPrimarySurface->SetRotation(m_nScreenWidth, m_nScreenHeight, angle);

    if (m_hRotateEvt)
    {
        SetEventData(m_hRotateEvt, m_iRotate);
        SetEvent(m_hRotateEvt);
    }
    
    return DISP_CHANGE_SUCCESSFUL;
}

/*****************************************************
 * Function: SetFullScreenModeToReg
 * Description: set overlay to normal/fullscreen mode
 * Calls: 
 * Called by: 
 * Table Access: list the table you will use. 
 * Table Update: list the table which you will update.
 * Input: int angle: FullScreen mode
 * Output: NONE
 * Return: NONE
 * Others: NONE
 ****************************************************/
void HisiDisp::SetFullScreenModeToReg(DWORD dwFullScreen)
{
    HKEY        hKey;

    if (ERROR_SUCCESS == RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("SYSTEM\\GDI\\ROTATION\\FULLSCREEN"), 
                                        0, 0, REG_OPTION_NON_VOLATILE, 0, NULL, &hKey, NULL))
    {
        if (ERROR_SUCCESS == RegSetValueEx(hKey,
                                             TEXT("FULLSCREEN"),
                                             NULL,
                                             REG_DWORD,
                                             (PBYTE)&dwFullScreen,
                                             sizeof(DWORD)))
        {
            DEBUGMSG(GPE_ZONE_HISI, (TEXT("Success to set full screen reg key.\n")));
            RETAILMSG(1, (TEXT("Set fullscreen reg: %d.\n"), dwFullScreen));
        }
        else
        {
            DISPMSG(1, (TEXT("Failed to set full screen reg key: %d.\n"), dwFullScreen));
        }
                
        if (RegCloseKey(hKey) != ERROR_SUCCESS )
        {
            DISPMSG(1, (TEXT("Error to close reg key.\n")));
        }
    }    
}

